package mbms.mobilebudget.income;

import java.util.List;

import mbms.mobilebudget.R;
import mbms.mobilebudget.bankaccount.BankAccountManager;
import mbms.mobilebudget.creditCard.CreditCardManager;
import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.BankAccount;
import mbms.mobilebudget.entity.CreditCard;
import mbms.mobilebudget.entity.Income;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.j256.ormlite.android.apptools.OrmLiteBaseListActivity;

@SuppressLint("ShowToast")
public class IncomesActivity extends OrmLiteBaseListActivity<DatabaseHandler> {

	private BankAccount acquiredBankAccount ; 
	private CreditCard acquiredCreditCard ;
	
	private BankAccountManager bankAccountManager;
	private CreditCardManager creditCardManager;
	private IncomeManager incomeManager;
	private IncomeAdapter incomeAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_incomes);
		
		Intent intent = getIntent();
		
		acquiredBankAccount = (BankAccount) intent.getSerializableExtra("passedBankAccount");
		acquiredCreditCard = (CreditCard) intent.getSerializableExtra("passedCreditCard");
		
		bankAccountManager = new BankAccountManager(getHelper(), getApplicationContext());
		creditCardManager =new CreditCardManager(getHelper(), getApplicationContext());
		incomeManager = new IncomeManager(getHelper(), getApplicationContext(), acquiredBankAccount, acquiredCreditCard);
		
		List<Income> incomeList;
		
		incomeList = getIncomeList();
		
		incomeAdapter = new IncomeAdapter(this, R.layout.income_row, incomeList);
		setListAdapter(incomeAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Income income = incomeAdapter.getItem(position);
				
				startIncomesAddUpdateActivity(income);
			}
		});
		
		registerForContextMenu(getListView());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.incomes, menu);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      Income income = incomeAdapter.getItem(info.position);

      switch(item.getItemId()) {
      case R.id.edit:
    	  startIncomesAddUpdateActivity(income);
            return true;
      case R.id.delete:
    	    incomeManager.deleteIncome(income);
    	    incomeAdapter.remove(income);
    	    
    	    setListAdapter(incomeAdapter);
    	    Toast.makeText(getApplicationContext(), "Delete successful", Toast.LENGTH_SHORT).show();
    	    return true;
      default:
            return super.onContextItemSelected(item);
      }
    }
	
	public void showInsertIncomeActivity(View view) {
		Income income = new Income();
		if(acquiredBankAccount != null) {
			income.setBankAccount(acquiredBankAccount);
		} else {
			income.setCreditCard(acquiredCreditCard);
		}
		
		startIncomesAddUpdateActivity(income);
	}
	
	public void backButtonClicked(View view){
		super.onBackPressed();
	} 
	
	private void startIncomesAddUpdateActivity(Income income) {
		Intent intent = new Intent(this, IncomesAddUpdateActivity.class);
		intent.putExtra("income", income);
		startActivity(intent);
	}
	
	@Override
    public void onResume()
    {
        super.onResume();
 		List<Income> incomeList = getIncomeList();
		
 		incomeAdapter = new IncomeAdapter(this, R.layout.income_row, incomeList);
 		setListAdapter(incomeAdapter);
 		
 		getListView().setOnItemClickListener(new OnItemClickListener() {
 			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Income income = incomeAdapter.getItem(position);
				
				startIncomesAddUpdateActivity(income);
			}
 		});
 		
 		registerForContextMenu(getListView());
    }
	 
	 private List<Income> getIncomeList() {
			List<Income> incomeList;
			if(acquiredBankAccount != null) {
				incomeList = bankAccountManager.getIncomeListOfBankAccount(acquiredBankAccount);
			} else {
				incomeList = creditCardManager.getIncomeListOfCreditCard(acquiredCreditCard);
			}
			return incomeList;
		}

}
